//
//  SearchViewController.m
//  听书吧
//
//  Created by 邹壮壮29 on 15-2-1.
//  Copyright (c) 2015年 蓝欧科技. All rights reserved.
//

#import "SearchViewController.h"
#import "AFNetworking.h"
#import "DBSphereView.h"
#import "RadioThirdViewController.h"
#import "SANetWorkingTask.h"
#import "SearchDetailViewController.h"
#import "MusicViewController.h"

@interface SearchViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)DBSphereView *spher;
@end

@implementation SearchViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.bigArray = [NSMutableArray array];
    }return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self myConnectData];
    self.view.backgroundColor = [UIColor whiteColor];
   [self searchField];
}

- (void)searchField
{
    //搜索框
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(self.view.frame.size.width/4, 0, self.view.frame.size.width/5, 44)];
    self.searchBar.placeholder = @"请输入搜索内容";
    self.navigationItem.titleView = self.searchBar;
    UIButton *searchButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width/20*17, 0, self.view.frame.size.width/20*3, 35)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:searchButton];

    [searchButton setTitle:@"搜索" forState:UIControlStateNormal];
    searchButton.backgroundColor = [UIColor colorWithRed:112/ 255.0 green:191 / 255.0 blue:234 / 255.0 alpha:0.6];
    searchButton.layer.cornerRadius = 15;
    searchButton.layer.masksToBounds = YES;
    [searchButton addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    [self creatLoadingView];
   
}
- (void)backAction:(id)sender
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    
}
- (void)searchAction:(id)sender{
    SearchDetailViewController *searchDetail = [[SearchDetailViewController alloc]init];
    //将汉字转换为NSUTF8StringEncoding格式
    NSString *searchContent = [self.searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    searchDetail.keyWord = searchContent;
    [self.navigationController pushViewController:searchDetail animated:YES];
}

- (void)myConnectData{
    NSString *string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=radio&condition=hot&device=iPhone&page=1&per_page=20&status=0&tag_name=都市"];
    string = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:string];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSData *data = operation.responseData;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [dict objectForKey:@"list"];
        [self.bigArray addObjectsFromArray:array];
        [self createView];
        [_indicator stopAnimating];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    [operation start];
}

//标签云
- (void)createView{
    self.spher = [[DBSphereView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.width/16, self.view.frame.size.width, self.view.frame.size.width)];
    
    NSMutableArray *array = [[NSMutableArray alloc]initWithCapacity:0];
    for (NSInteger i = 0; i < [self.bigArray count]; i++) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 40)];
        NSDictionary *dic = [self.bigArray objectAtIndex:i];
        [button setTitle:[dic objectForKey:@"title"] forState:UIControlStateNormal];
        
        //随机生成颜色
        CGFloat red = arc4random()%256/256.0f;
        CGFloat green = arc4random()%256/256.0f;
        CGFloat blue = arc4random()%256/256.0f;
        [button setTitleColor:[UIColor colorWithRed:red green:green blue:blue alpha:1.0] forState:UIControlStateNormal];
        
        button.titleLabel.font = [UIFont systemFontOfSize:20];
        [button.titleLabel setNumberOfLines:0];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        button.tag = [[dic objectForKey:@"id"] integerValue];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [array addObject:button];
        [self.spher addSubview:button];
    }
    [self.spher setCloudTags:array];
    self.spher.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.spher];
}

- (void)buttonAction:(UIButton *)btn
{
    [self.spher timerStop];
    [UIView animateWithDuration:0.2 animations:^{
        btn.transform = CGAffineTransformMakeScale(2., 2.);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{
            btn.transform = CGAffineTransformMakeScale(1., 1.);
        } completion:^(BOOL finished) {
            [self.spher timerStart];
        }];
    }];
    RadioThirdViewController *third = [[RadioThirdViewController alloc]init];
    third.myId = [NSNumber numberWithInteger:btn.tag];
    [self.navigationController pushViewController:third animated:YES];

}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.searchBar resignFirstResponder];
    
}

//创建风火轮
- (void)creatLoadingView
{
    self.indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
    //设置显示样式,见UIActivityIndicatorViewStyle的定义
    _indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    //设置显示位置
    _indicator.center = self.view.center;
    _indicator.backgroundColor = [UIColor grayColor];
    [self.view bringSubviewToFront:_indicator];
    //将初始化好的indicator add到view中
    [self.view addSubview:_indicator];
    //开始显示Loading动画
    [_indicator startAnimating];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
